﻿Imports Business
Imports DTO
Imports System.Globalization

Public Class frmTimetable
    Dim bsTimetable As BSTimetable
    ' data table cache
    Dim dtTimetable As DataTable

    Dim dtDgv As DataTable

    ' provider datetime
    Dim provider As CultureInfo

    ' current timetable when user selected in datagridview
    Dim dtoCurrTimetable As DTOTimetable

    ' flag edited restTime
    Dim editedResttime As Boolean

    ' string column title datagridview
    Dim scolTimetableNo As String = "Timetable No"
    Dim scolExFrom As String = "Expiration From"
    Dim scolExTo As String = "Expiration To"
    Dim scolTimetableName As String = "Timetable Name"
    Dim scolAbre As String = "Abreviation"
    Dim scolWorkFrom As String = "Work From"
    Dim scolWorkTo As String = "Work To"
    Dim scolDelimitation As String = "Delimitation"
    Dim scolMidnightFrom As String = "Midnight From"
    Dim scolMidnightTo As String = "Midnight To"
    Dim scolUnit As String = "Unit Minutes"
    Dim scolOTUnit As String = "Overtime Unit Minutes"


    ' string label timetable detail in groupbox
    Dim slblTimetableNo As String
    Dim slblExFrom As String
    Dim slblExTo As String
    Dim slblTimetableName As String
    Dim slblAbrev As String
    Dim slblWorkFrom As String
    Dim slblWorkTo As String
    Dim slblDelimitation As String
    Dim slblMidFrom As String
    Dim slblMidTo As String
    Dim slblUnit As String
    Dim slblOtUnit As String
    Dim slblRestime As String
    Dim scolRestNo As String = "Rest No"
    Dim scolRestFrom As String = "Rest From"
    Dim scolRestTo As String = "Rest To"
    Dim sbtnAdd As String
    Dim sbtnDelete As String
    Dim sbtnUpdate As String
    Dim sbtnCancel As String
    Dim schkLock As String
    Dim schkShow As String
    Dim sgrpDetail As String
    Dim normalMessage As String = "Format hour is 'hh:mm' and format day is 'dd/MM/yyyy'."
    Dim erDelimit1 As String = "Input work from and work to before input delimitation."
    Dim erDelimit2 As String = "Delimitation must be in range work from and work to."
    Dim erDelimit3 As String = "Delimitation must be greater than work from."
    Dim erDelimit4 As String = "Delimitation must be less than than work to."
    Dim erWorkTo1 As String = "Work to must be greater than work form."
    Dim erWrongFormat As String = "Input data is wrong."
    Dim erWorkFrom1 As String = "Work from must be less than work to."
    Dim erExTo1 As String = "Expiration date to must be greater than expiration date from."
    Dim erExFrom1 As String = "Expiration date from must be less than expiration date to."
    Dim erResttime1 As String = "Rest time from must be less than rest to."
    Dim erResttime2 As String = "Rest time to must be greater than rest time from."

    Dim yearFrom As Integer = 2000
    Dim yearTo As Integer = 2020

    Public Sub New(ByVal strServer As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' provider
        provider = New CultureInfo("fr-FR")

        bsTimetable = New BSTimetable(strServer)
        '  dgvTimetable.DataSource = bsTimetable.GetTimetable()
        dtTimetable = bsTimetable.GetTimetable()

        ' Create new datatable
        dtDgv = New DataTable()

        ' create current timetable
        dtoCurrTimetable = New DTOTimetable()

        ' Template column
        Dim cell As DataGridViewCell = New DataGridViewTextBoxCell()

        ' set message suggestion
        lblMessage.Text = normalMessage
        '--------------------------------------add column datagridview timetable-------------------------------------
        ' add column timtable no
        Dim colTimetableNo As DataGridViewColumn = New DataGridViewColumn
        colTimetableNo.HeaderText = scolTimetableNo
        colTimetableNo.Name = scolTimetableNo
        colTimetableNo.ReadOnly = True
        colTimetableNo.CellTemplate = cell
        grdTimetable.Columns.Add(colTimetableNo)

        ' add column expiration from
        Dim colExFrom As DataGridViewColumn = New DataGridViewColumn
        colExFrom.HeaderText = scolExFrom
        colExFrom.Name = scolExFrom
        colExFrom.ReadOnly = True
        colExFrom.CellTemplate = cell
        grdTimetable.Columns.Add(colExFrom)

        ' add column expiration to
        Dim colExTo As DataGridViewColumn = New DataGridViewColumn
        colExTo.HeaderText = scolExTo
        colExTo.Name = scolExTo
        colExTo.ReadOnly = True
        colExTo.CellTemplate = cell
        grdTimetable.Columns.Add(colExTo)

        'add column timetable name
        Dim colTimetableName As DataGridViewColumn = New DataGridViewColumn
        colTimetableName.HeaderText = scolTimetableName
        colTimetableName.Name = scolTimetableName
        colTimetableName.ReadOnly = True
        colTimetableName.CellTemplate = cell
        grdTimetable.Columns.Add(colTimetableName)

        ' add column Abreviation
        Dim colAbrev As DataGridViewColumn = New DataGridViewColumn
        colAbrev.HeaderText = scolAbre
        colAbrev.Name = scolAbre
        colAbrev.ReadOnly = True
        colAbrev.CellTemplate = cell
        grdTimetable.Columns.Add(colAbrev)

        ' add column work from
        Dim colWorkFrom As DataGridViewColumn = New DataGridViewColumn
        colWorkFrom.HeaderText = scolWorkFrom
        colWorkFrom.Name = scolWorkFrom
        colWorkFrom.ReadOnly = True
        colWorkFrom.CellTemplate = cell
        grdTimetable.Columns.Add(colWorkFrom)

        ' add column work to
        Dim colWorkTo As DataGridViewColumn = New DataGridViewColumn
        colWorkTo.HeaderText = scolWorkTo
        colWorkTo.Name = scolWorkTo
        colWorkTo.ReadOnly = True
        colWorkTo.CellTemplate = cell
        grdTimetable.Columns.Add(colWorkTo)

        ' add column delimitation
        Dim colDelimitation As DataGridViewColumn = New DataGridViewColumn
        colDelimitation.HeaderText = scolDelimitation
        colDelimitation.Name = scolDelimitation
        colDelimitation.ReadOnly = True
        colDelimitation.CellTemplate = cell
        grdTimetable.Columns.Add(colDelimitation)

        '' add column midnight from
        'Dim colMidFrom As DataGridViewColumn = New DataGridViewColumn
        'colMidFrom.HeaderText = scolMidnightFrom
        'colMidFrom.Name = scolMidnightFrom
        'colMidFrom.ReadOnly = True
        'colMidFrom.CellTemplate = cell
        'grdTimetable.Columns.Add(colMidFrom)

        '' add column midnight to
        'Dim colMidTo As DataGridViewColumn = New DataGridViewColumn
        'colMidTo.HeaderText = scolMidnightTo
        'colMidTo.Name = scolMidnightTo
        'colMidTo.ReadOnly = True
        'colMidTo.CellTemplate = cell
        'grdTimetable.Columns.Add(colMidTo)

        ' add column unit minutes
        Dim colUnit As DataGridViewColumn = New DataGridViewColumn
        colUnit.HeaderText = scolUnit
        colUnit.Name = scolUnit
        colUnit.ReadOnly = True
        colUnit.CellTemplate = cell
        grdTimetable.Columns.Add(colUnit)

        ' add column overtime unit minutes
        Dim colOtUnit As DataGridViewColumn = New DataGridViewColumn
        colOtUnit.HeaderText = scolOTUnit
        colOtUnit.Name = scolOTUnit
        colOtUnit.ReadOnly = True
        colOtUnit.CellTemplate = cell
        grdTimetable.Columns.Add(colOtUnit)

        '--------------------------------------add column to datagridview Rest Time--------------------------------------------
        '' add column rest no
        'Dim colRestNo As DataGridViewColumn = New DataGridViewColumn()
        'colRestNo.HeaderText = scolRestNo
        'colRestNo.Name = scolRestNo
        'colRestNo.CellTemplate = cell
        'grdRestTime.Columns.Add(colRestNo)

        '' add column  no
        'Dim colNo As DataGridViewColumn = New DataGridViewColumn()
        'colNo.HeaderText = scolNo
        'colNo.Name = scolNo
        'colNo.CellTemplate = cell
        'grdRestTime.Columns.Add(colNo)

        ' add column rest from
        Dim colRestFrom As DataGridViewColumn = New DataGridViewColumn
        colRestFrom.HeaderText = scolRestFrom
        colRestFrom.Name = scolRestFrom
        colRestFrom.CellTemplate = cell
        grdRestTime.Columns.Add(colRestFrom)

        ' add column rest to
        Dim colRestTo As DataGridViewColumn = New DataGridViewColumn
        colRestTo.HeaderText = scolRestTo
        colRestTo.Name = scolRestTo
        colRestTo.CellTemplate = cell
        grdRestTime.Columns.Add(colRestTo)

        '---------------------------------------------------------------------------------------------


        dtDgv.Columns.Add(scolTimetableNo)
        dtDgv.Columns.Add(scolExFrom)
        dtDgv.Columns.Add(scolExTo)
        dtDgv.Columns.Add(scolTimetableName)
        dtDgv.Columns.Add(scolAbre)
        dtDgv.Columns.Add(scolWorkFrom)
        dtDgv.Columns.Add(scolWorkTo)
        dtDgv.Columns.Add(scolDelimitation)
        dtDgv.Columns.Add(scolMidnightFrom)
        dtDgv.Columns.Add(scolMidnightTo)
        dtDgv.Columns.Add(scolUnit)
        dtDgv.Columns.Add(scolOTUnit)

        LoadGrdTimetable()

        ' disable column header sorting
        For Each column As DataGridViewColumn In grdTimetable.Columns
            column.SortMode = DataGridViewColumnSortMode.NotSortable
        Next



    End Sub



    Private Sub LoadGrdTimetable()

        ' add row in dtResult
        If chkShow.Checked Then
            AddAllTimetable() ' dtDgv thay doi trong sub nay
        Else
            AddTimetableExist()
        End If

    End Sub


    Private Sub chkShow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShow.CheckedChanged
        LoadGrdTimetable()
    End Sub


    ''' <summary>
    ''' Add all row to datagridview and set color row deleted
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddAllTimetable()
        If grdTimetable.Rows.Count > 0 Then
            grdTimetable.Rows.Clear()
        End If

        For Each row As DataRow In dtTimetable.Rows
            ' create new row 
            'Dim newRow As DataRow = dtDgv.NewRow()
            Dim newRow As DataGridViewRow = New DataGridViewRow()
            AddRowTimetable(newRow, row)
            grdTimetable.Rows.Add(newRow)

        Next
        ' set datasource dtDgv on datagridview
        'grdTimetable.DataSource = dtDgv
        'SetDataSourceGridTimetable(dtDgv)
        ' set back color row is deleted
        For i As Integer = 0 To dtTimetable.Rows.Count - 1
            If dtTimetable.Rows.Item(i).Item("STATUS") = 1 Then
                grdTimetable.Rows.Item(i).DefaultCellStyle.BackColor = Color.DarkGray
            End If
        Next

    End Sub

    Private Sub AddTimetableExist()

        If grdTimetable.Rows.Count > 0 Then
            grdTimetable.Rows.Clear()
        End If
        For Each row As DataRow In dtTimetable.Rows
            If row.Item("STATUS").ToString().Equals("0") Then
                ' create new row
                Dim newRow As DataGridViewRow = New DataGridViewRow()

                AddRowTimetable(newRow, row)
                grdTimetable.Rows.Add(newRow)

            End If
        Next
        ' set datasource dtDgv on datagridview
        ' grdTimetable.DataSource = dtDgv
        'SetDataSourceGridTimetable(dtDgv)

    End Sub

    Private Sub AddRowTimetable(ByVal newRow As DataGridViewRow, ByVal row As DataRow)

        Dim cell As DataGridViewCell = New DataGridViewTextBoxCell()

        ' set cell value
        ' timtable no
        cell.Value = row.Item("TIME_TABLE_NO")
        newRow.Cells.Add(cell)

        ' expiration from
        cell = New DataGridViewTextBoxCell()
        If Not row.Item("EXPIRATION_FROM").ToString().Equals("0") Then
            cell.Value = Functions.ConvertstrDBDateToStrDate((row.Item("EXPIRATION_FROM")))
            newRow.Cells.Add(cell)
        Else
            cell.Value = ""
            newRow.Cells.Add(cell)
        End If

        ' expiration to
        cell = New DataGridViewTextBoxCell()
        If Not row.Item("EXPIRATION_TO").ToString().Equals("99999999") Then
            cell.Value = Functions.ConvertstrDBDateToStrDate((row.Item("EXPIRATION_TO")))
            newRow.Cells.Add(cell)
        Else
            cell.Value = ""
            newRow.Cells.Add(cell)
        End If

        ' Timetable name
        cell = New DataGridViewTextBoxCell()
        cell.Value = row.Item("TIME_TABLE_NAME")
        newRow.Cells.Add(cell)

        ' Timetable abreviation
        cell = New DataGridViewTextBoxCell()
        If Not IsDBNull(row.Item("ABREVIATION")) Then
            cell.Value = row.Item("ABREVIATION")
        Else
            cell.Value = ""
        End If
        newRow.Cells.Add(cell)

        ' Timetable work from
        cell = New DataGridViewTextBoxCell()
        cell.Value = Double.Parse(row.Item("WORK_FROM")).ToString("##:##")
        newRow.Cells.Add(cell)

        ' Timetable work to
        cell = New DataGridViewTextBoxCell()
        cell.Value = Double.Parse(row.Item("WORK_TO")).ToString("##:##")
        newRow.Cells.Add(cell)

        ' Timetable delimitation
        cell = New DataGridViewTextBoxCell()
        cell.Value = Double.Parse(row.Item("DELIMITATION")).ToString("##:##")
        newRow.Cells.Add(cell)

        '' Timetable midnight work from
        'cell = New DataGridViewTextBoxCell()
        'If Not IsDBNull(row.Item("MIDNIGHT_WORK_FROM")) Then
        '    cell.Value = Double.Parse(row.Item("MIDNIGHT_WORK_FROM")).ToString("##:##")
        'Else
        '    cell.Value = ""
        'End If
        'newRow.Cells.Add(cell)

        '' Timetable midnight work to
        'cell = New DataGridViewTextBoxCell()
        'If Not IsDBNull(row.Item("MIDNIGHT_WORK_TO")) Then
        '    cell.Value = Double.Parse(row.Item("MIDNIGHT_WORK_TO")).ToString("##:##")
        'Else
        '    cell.Value = ""
        'End If
        'newRow.Cells.Add(cell)

        ' Timetable unit minutes
        cell = New DataGridViewTextBoxCell()
        cell.Value = row.Item("UNIT_MINUTES")
        newRow.Cells.Add(cell)

        ' Timetable overtime unit minutes
        cell = New DataGridViewTextBoxCell()
        cell.Value = row.Item("OVER_UNIT_MINUTES")
        newRow.Cells.Add(cell)

    End Sub

    Private Sub chkLock_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkLock.CheckedChanged

        If chkLock.Checked Then
            'disable all textbox detail
            DisableDetails()
        Else
            ' enable all text detail
            If grdTimetable.SelectedRows.Count < 1 Then
                ' allow add and ignore modify, delete timetable
                EnableDetails()
                btnModify.Enabled = False
                btnDelete.Enabled = False

            Else
                ' allow modify, delete and ignore add timetable. don't allow to change timetable no
                EnableDetails()
                btnAdd.Enabled = False
                txtTimetableNo.ReadOnly = True
            End If

        End If

    End Sub

    Private Sub DisableDetails()

        txtTimetableNo.ReadOnly = True
        txtTimetableName.ReadOnly = True
        txtExFrom.ReadOnly = True
        txtExTo.ReadOnly = True
        txtAbrev.ReadOnly = True
        txtDelimitation.ReadOnly = True
        txtMidFrom.ReadOnly = True
        txtMidTo.ReadOnly = True
        txtWorkFrom.ReadOnly = True
        txtWorkTo.ReadOnly = True

        grdRestTime.ReadOnly = True

        btnAdd.Enabled = False
        btnClear.Enabled = False
        btnDelete.Enabled = False
        btnModify.Enabled = False

        cboOtUnit.Enabled = False
        cboUnit.Enabled = False

    End Sub

    Private Sub EnableDetails()

        txtTimetableNo.ReadOnly = False
        txtTimetableName.ReadOnly = False
        txtExFrom.ReadOnly = False
        txtExTo.ReadOnly = False
        txtAbrev.ReadOnly = False
        txtDelimitation.ReadOnly = False
        txtMidFrom.ReadOnly = False
        txtMidTo.ReadOnly = False
        txtWorkFrom.ReadOnly = False
        txtWorkTo.ReadOnly = False

        grdRestTime.ReadOnly = False

        btnAdd.Enabled = True
        btnClear.Enabled = True
        btnDelete.Enabled = True
        btnModify.Enabled = True

        cboOtUnit.Enabled = True
        cboUnit.Enabled = True

    End Sub

    ''' <summary>
    ''' Event handle user selected a timetable on datagridview
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdTimetable_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdTimetable.CellClick

        If Not txtTimetableNo.ReadOnly Then

            txtTimetableNo.ReadOnly = True

        End If

        If btnAdd.Enabled Then

            btnAdd.Enabled = False

            btnModify.Enabled = True
            btnDelete.Enabled = True

        End If


        ' fill in details panel
        txtTimetableNo.Text = grdTimetable.CurrentRow.Cells.Item(scolTimetableNo).Value

        txtTimetableName.Text = grdTimetable.CurrentRow.Cells.Item(scolTimetableName).Value

        txtExFrom.Text = grdTimetable.CurrentRow.Cells.Item(scolExFrom).Value

        txtExTo.Text = grdTimetable.CurrentRow.Cells.Item(scolExTo).Value

        txtWorkFrom.Text = grdTimetable.CurrentRow.Cells.Item(scolWorkFrom).Value

        txtWorkTo.Text = grdTimetable.CurrentRow.Cells.Item(scolWorkTo).Value

        'txtMidFrom.Text = grdTimetable.CurrentRow.Cells.Item(scolMidnightFrom).Value

        'txtMidTo.Text = grdTimetable.CurrentRow.Cells.Item(scolMidnightTo).Value

        txtDelimitation.Text = grdTimetable.CurrentRow.Cells.Item(scolDelimitation).Value

        txtAbrev.Text = grdTimetable.CurrentRow.Cells.Item(scolAbre).Value

        cboOtUnit.Text = grdTimetable.CurrentRow.Cells.Item(scolOTUnit).Value

        cboUnit.Text = grdTimetable.CurrentRow.Cells.Item(scolUnit).Value

        ' get dto timetable current without rest time
        GetDtoTimetable(dtoCurrTimetable)

        ' load rest time datagridview
        LoadRestTime(bsTimetable.GetRestTime(dtoCurrTimetable))

        ' get rest time for dto current timetable
        GetRestTimeToDto(dtoCurrTimetable)

        'deselect all rows in datagridview 
        grdRestTime.ClearSelection()

    End Sub

    Public Sub LoadRestTime(ByVal dataTable As DataTable)

        If grdRestTime.Rows.Count > 0 Then

            grdRestTime.Rows.Clear()

        End If

        If Not dataTable Is Nothing Then

            Dim newRow As DataGridViewRow

            For Each row As DataRow In dataTable.Rows

                newRow = New DataGridViewRow

                Dim cell As DataGridViewCell = New DataGridViewTextBoxCell()

                'cell.Value = row.Item("REST_NO")
                'newRow.Cells.Add(cell)
                ' cell resttime from

                cell = New DataGridViewTextBoxCell()

                cell.Value = Double.Parse(row.Item("REST_FROM")).ToString("##:##")

                newRow.Cells.Add(cell)

                'cell resttime to
                cell = New DataGridViewTextBoxCell()

                cell.Value = Double.Parse(row.Item("REST_TO")).ToString("##:##")

                newRow.Cells.Add(cell)

                grdRestTime.Rows.Add(newRow)

            Next

        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        frmMain.RemoveTabpage()

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        If txtTimetableNo.ReadOnly Then

            txtTimetableNo.ReadOnly = False

        End If

        If btnModify.Enabled Then

            btnModify.Enabled = False

            btnDelete.Enabled = False

            btnAdd.Enabled = True

        End If

        ClearDetails()

    End Sub

    Private Sub ClearDetails()

        txtTimetableNo.Text = Nothing
        txtTimetableName.Text = Nothing
        txtExFrom.Text = Nothing
        txtExTo.Text = Nothing
        txtWorkFrom.Text = Nothing
        txtWorkTo.Text = Nothing
        txtMidFrom.Text = Nothing
        txtMidTo.Text = Nothing
        txtDelimitation.Text = Nothing
        txtAbrev.Text = Nothing

        cboOtUnit.Text = Nothing
        cboUnit.Text = Nothing

        If grdRestTime.Rows.Count > 0 Then
            grdRestTime.Rows.Clear()
        End If

    End Sub


    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        ' create a data tranfer object timetable
        Dim dtoTimetable As DTOTimetable = New DTOTimetable()

        ' flag = true if dto timetable is valid 
        Dim flag As Boolean = True

        ' validate txt timetable no
        If Not String.IsNullOrEmpty(txtTimetableNo.Text) Then

            If Not CheckDuplicateTimetableNo(Integer.Parse(txtTimetableNo.Text.Trim())) Then

                ' timetable no
                dtoTimetable.TimetableNoProperty = txtTimetableNo.Text

                ' clear message if had
                lblMessage.Text = Nothing


                ' validate  txt timetable name
                If Not String.IsNullOrEmpty(txtTimetableName.Text) Then

                    ' timetable name
                    dtoTimetable.TimetableNameProperty = txtTimetableName.Text

                    ' clear message if had
                    lblMessage.Text = Nothing


                    'validate txt work from 
                    If Not String.IsNullOrEmpty(txtWorkFrom.Text) Then

                        ' timetable work from
                        dtoTimetable.WorkFromProperty = Functions.ConvertStrHourToInt(txtWorkFrom.Text)

                        ' clear message if had
                        lblMessage.Text = Nothing


                        ' validate txt work to
                        If Not String.IsNullOrEmpty(txtWorkTo.Text) Then

                            ' timetable work to
                            dtoTimetable.WorkToProperty = Functions.ConvertStrHourToInt(txtWorkTo.Text)

                            ' clear message if had
                            lblMessage.Text = Nothing


                            ' validate txt delimitation
                            If Not String.IsNullOrEmpty(txtDelimitation.Text) Then

                                ' timetable delimitation
                                dtoTimetable.DelimitationProperty = Functions.ConvertStrHourToInt(txtDelimitation.Text)

                                ' clear message if had
                                lblMessage.Text = Nothing


                                ' validate cbo unit minute
                                If Not String.IsNullOrEmpty(cboUnit.Text) Then

                                    ' unit minute
                                    dtoTimetable.UnitProperty = cboUnit.Text

                                    ' clear message if had
                                    lblMessage.Text = Nothing


                                    ' validate cbo  OT unit minute
                                    If Not String.IsNullOrEmpty(cboOtUnit.Text) Then

                                        'OT unit minute
                                        dtoTimetable.OtUnitProperty = cboOtUnit.Text

                                        ' clear message if had
                                        lblMessage.Text = Nothing


                                        ' timetable rest time
                                        If ValidateInputRestTime() Then

                                            For i As Integer = 0 To grdRestTime.Rows.Count - 2

                                                'create new row
                                                Dim row As DataRow = dtoTimetable.DtRestTimeProperty.NewRow()

                                                ' rest from
                                                row.Item(0) = Functions.ConvertStrHourToInt(grdRestTime.Rows.Item(i).Cells.Item(0).Value)
                                                Console.WriteLine(row.Item(0))

                                                ' rest to
                                                row.Item(1) = Functions.ConvertStrHourToInt(grdRestTime.Rows.Item(i).Cells.Item(1).Value)
                                                Console.WriteLine(row.Item(1))

                                                ' add row
                                                dtoTimetable.DtRestTimeProperty.Rows.Add(row)

                                            Next

                                            ' clear message if had
                                            lblMessage.Text = Nothing


                                            ' timetable abbreviation
                                            If Not String.IsNullOrEmpty(txtAbrev.Text) Then

                                                dtoTimetable.AbreviationProperty = txtAbrev.Text

                                            Else

                                                dtoTimetable.AbreviationProperty = "NULL"

                                            End If

                                            ' timetable expiration from
                                            If Not String.IsNullOrEmpty(txtExFrom.Text) Then

                                                dtoTimetable.ExFromProperty = Functions.ConvertStrDateToInt(txtExFrom.Text)

                                            Else

                                                dtoTimetable.ExFromProperty = "0"

                                            End If

                                            ' timetable expiration to
                                            If Not String.IsNullOrEmpty(txtExTo.Text) Then

                                                dtoTimetable.ExToProperty = Functions.ConvertStrDateToInt(txtExTo.Text)

                                            Else

                                                dtoTimetable.ExToProperty = "99999999"

                                            End If

                                            ' timetable midnight from
                                            If Not String.IsNullOrEmpty(txtMidFrom.Text) Then

                                                dtoTimetable.MidFromProperty = Functions.ConvertStrHourToInt(txtMidFrom.Text)

                                            Else

                                                dtoTimetable.MidFromProperty = "NULL"

                                            End If

                                            ' timetable midnight to
                                            If Not String.IsNullOrEmpty(txtMidTo.Text) Then

                                                dtoTimetable.MidToProperty = Functions.ConvertStrHourToInt(txtMidTo.Text)

                                            Else

                                                dtoTimetable.MidToProperty = "NULL"

                                            End If

                                        Else

                                            ' set flag 
                                            flag = False

                                            ' print message
                                            lblMessage.Text = "Rest time is not enough information."

                                        End If

                                    Else

                                        ' set flag 
                                        flag = False

                                        ' print message and focus on it
                                        cboOtUnit.Focus()

                                        lblMessage.Text = "'OT Unit Minute' is not empty."

                                    End If

                                Else

                                    ' set flag 
                                    flag = False

                                    ' print message and focus on it
                                    cboUnit.Focus()

                                    lblMessage.Text = "'Unit Minute' is not empty."

                                End If

                            Else

                                ' set flag 
                                flag = False

                                ' print message and focus on it
                                txtDelimitation.Focus()

                                lblMessage.Text = "'Delimitation' is not empty."

                            End If

                        Else

                            ' set flag 
                            flag = False

                            ' print message and focus on it
                            txtWorkTo.Focus()

                            lblMessage.Text = "'Work To' is not empty."

                        End If

                    Else

                        ' set flag 
                        flag = False

                        ' print message and focus on it
                        txtWorkFrom.Focus()

                        lblMessage.Text = "'Work From' is not empty."

                    End If

                Else

                    ' set flag 
                    flag = False

                    ' print message and focus on it
                    txtTimetableName.Focus()

                    lblMessage.Text = "'Timetable Name' is not empty."

                End If

            Else

                ' set flag 
                flag = False

                ' print message and focus on it
                txtTimetableNo.Focus()

                lblMessage.Text = "'Timetable No' is duplicated."

            End If

        Else

            ' set flag 
            flag = False

            ' print message and focus on it
            txtTimetableNo.Focus()

            lblMessage.Text = "'Timetable No' is not empty."

        End If


        ' check dto timetable is valid
        If flag Then

            If bsTimetable.AddTimetable(dtoTimetable) Then

                ' print message success
                lblMessage.Text = "Add Timetable success."

                ' reload dtTimetable
                dtTimetable = bsTimetable.GetTimetable()

                'reload datagridview timetable
                LoadGrdTimetable()

                ' clear details timetable
                ClearDetails()

            Else

                lblMessage.Text = "Add Timetable error."

            End If


        End If


    End Sub




    'Public Function ValidateInputTimetable() As Boolean

    '    If Not String.IsNullOrEmpty(txtTimetableNo.Text) And Not String.IsNullOrEmpty(txtTimetableName.Text) And Not String.IsNullOrEmpty(txtWorkFrom.Text) _
    '        And Not String.IsNullOrEmpty(txtWorkTo.Text) And Not String.IsNullOrEmpty(txtDelimitation.Text) Then
    '        Return True
    '    Else
    '        Return False
    '    End If

    'End Function

    ''' <summary>
    ''' Function check duplicate timetable No
    ''' </summary>
    ''' <param name="timetableNo"></param>
    ''' <returns>True: if duplicate
    ''' False: if not</returns>
    ''' <remarks></remarks>
    Public Function CheckDuplicateTimetableNo(ByVal timetableNo As Integer) As Boolean

        For Each row As DataRow In dtTimetable.Rows

            If Integer.Parse(row.Item("TIME_TABLE_NO").ToString()) = timetableNo Then

                Return True

            End If

            Return False

        Next

    End Function


    ''' <summary>
    ''' Function validate Rest time
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ValidateInputRestTime() As Boolean

        For i As Integer = 0 To grdRestTime.Rows.Count - 2

            If grdRestTime.Rows.Item(i).Cells.Item(0).Value Is Nothing Then

                If Not grdRestTime.Rows.Item(i).Cells.Item(1).Value Is Nothing Then

                    Return False

                End If

            Else

                If grdRestTime.Rows.Item(i).Cells.Item(1).Value Is Nothing Then

                    Return False

                End If

            End If




        Next

        Return True

    End Function


    ''' <summary>
    ''' Event Load form Timetable
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub frmTimetable_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' deselect all rows in datagridview Timetable
        grdTimetable.ClearSelection()
    End Sub

    ''' <summary>
    ''' only allow input is numeric Expiration from
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtExFrom_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtExFrom.KeyPress

        ' limit length of text less than 8 letters
        txtExFrom.MaxLength = 8

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub


    Private Sub txtExFrom_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtExFrom.Enter

        ' if textbox is unlock
        If Not chkLock.Checked Then

            '' set message 
            'lblMessage.Text = "Format dd/MM/yyyy"

            'check if string text in textbox is different null 
            If Not String.IsNullOrWhiteSpace(txtExFrom.Text) Then

                ' convert string date to integer data
                txtExFrom.Text = ConvertStrDateToInt(txtExFrom.Text)

            End If

        End If

    End Sub

    ''' <summary>
    ''' Event handle when go way from Expiration From textbox
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtExFrom_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtExFrom.Leave

        ' if it's unlocked, we validate data
        If Not chkLock.Checked Then

            If Not String.IsNullOrWhiteSpace(txtExFrom.Text) And txtExFrom.Text.Length >= 7 Then

                ' validate year, month, day
                If Functions.ValidateStrDate(Double.Parse(txtExFrom.Text.Trim()).ToString("0#/##/####"), yearFrom, yearTo) Then

                    If Not String.IsNullOrEmpty(txtExTo.Text) Then

                        If DateTime.ParseExact(Double.Parse(txtExFrom.Text.Trim()).ToString("0#/##/####"), "d", New CultureInfo("fr-FR")) _
                            < DateTime.ParseExact(txtExTo.Text, "d", New CultureInfo("fr-FR")) Then

                            '  set value to textbox
                            txtExFrom.Text = Double.Parse(txtExFrom.Text).ToString("0#/##/####")

                            'set normal message
                            lblMessage.Text = normalMessage

                        Else

                            ' false clear value
                            txtExFrom.Text = Nothing

                            ' print error message
                            lblMessage.Text = erExFrom1

                        End If

                    Else

                        '  set value to textbox
                        txtExFrom.Text = Double.Parse(txtExFrom.Text).ToString("0#/##/####")

                        'set normal message
                        lblMessage.Text = normalMessage

                    End If


                Else

                    ' false clear value
                    txtExFrom.Text = Nothing

                    ' print error message
                    lblMessage.Text = erWrongFormat

                End If
            Else

                ' false clear value
                txtExFrom.Text = Nothing

            End If

            '' set value for message is null
            'lblMessage.Text = Nothing

        End If

    End Sub



    Private Sub txtExTo_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtExTo.Enter

        ' if textbox is unlock
        If Not chkLock.Checked Then

            '' set message 
            'lblMessage.Text = "Format dd/MM/yyyy"

            'check if string text in textbox is different null 
            If Not String.IsNullOrWhiteSpace(txtExTo.Text) Then

                ' convert string date to integer data
                txtExTo.Text = Functions.ConvertStrDateToInt(txtExTo.Text)

            End If

        End If

    End Sub

    Private Sub txtExTo_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtExTo.KeyPress

        ' limit length of text less than 8 letters
        txtExTo.MaxLength = 8

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub

    Private Sub txtExTo_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtExTo.Leave

        ' if it's unlocked, we validate data
        If Not chkLock.Checked Then

            If Not String.IsNullOrWhiteSpace(txtExTo.Text) And txtExTo.Text.Length >= 7 Then

                ' validate year, month, day
                If Functions.ValidateStrDate(Double.Parse(txtExTo.Text.Trim()).ToString("0#/##/####"), yearFrom, yearTo) Then

                    If Not String.IsNullOrEmpty(txtExFrom.Text) Then

                        If DateTime.ParseExact(Double.Parse(txtExTo.Text.Trim()).ToString("0#/##/####"), "d", New CultureInfo("fr-FR")) _
                            > DateTime.ParseExact(txtExFrom.Text, "d", New CultureInfo("fr-FR")) Then

                            ' set value to textbox
                            txtExTo.Text = Double.Parse(txtExTo.Text).ToString("0#/##/####")

                            'set normal message
                            lblMessage.Text = normalMessage
                        Else

                            ' false, clear value
                            txtExTo.Text = Nothing

                            ' print error message
                            lblMessage.Text = erExTo1

                        End If

                    Else

                        ' set value to textbox
                        txtExTo.Text = Double.Parse(txtExTo.Text).ToString("0#/##/####")

                        'set normal message
                        lblMessage.Text = normalMessage

                    End If


                Else

                    ' false clear value
                    txtExTo.Text = Nothing

                    ' print error message
                    lblMessage.Text = erWrongFormat

                End If
            Else

                ' false clear value
                txtExTo.Text = Nothing

            End If

            '' set value for message is null
            'lblMessage.Text = Nothing

        End If

    End Sub

    Private Sub txtWorkFrom_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtWorkFrom.KeyPress

        ' limit length of text less than 4 letters
        txtWorkFrom.MaxLength = 4

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub

    Private Sub txtWorkFrom_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWorkFrom.Leave

        ' if it's unlocked, we validate data
        If Not chkLock.Checked Then

            If Not String.IsNullOrWhiteSpace(txtWorkFrom.Text) And txtWorkFrom.Text.Length >= 3 Then

                ' validate hour and minute
                If Functions.ValidateStrHour(Double.Parse(txtWorkFrom.Text.Trim()).ToString("0#:##")) Then

                    If Not String.IsNullOrEmpty(txtWorkTo.Text) Then

                        If Integer.Parse(txtWorkFrom.Text) < Integer.Parse(Functions.ConvertStrHourToInt(txtWorkTo.Text)) Then

                            ' true, set value to textbox
                            txtWorkFrom.Text = Double.Parse(txtWorkFrom.Text).ToString("0#:##")

                            'set normal message
                            lblMessage.Text = normalMessage

                        Else

                            ' false clear value
                            txtWorkFrom.Text = Nothing

                            ' print error message
                            lblMessage.Text = erWorkFrom1

                        End If

                    Else

                        ' set value to textbox
                        txtWorkFrom.Text = Double.Parse(txtWorkFrom.Text).ToString("0#:##")

                        'set normal message
                        lblMessage.Text = normalMessage

                    End If

                Else

                    ' false clear value
                    txtWorkFrom.Text = Nothing

                    'print error message
                    lblMessage.Text = erWrongFormat

                End If
            Else

                ' false clear value
                txtWorkFrom.Text = Nothing

            End If

            '' set value for message is null
            'lblMessage.Text = Nothing

        End If

    End Sub

    Private Sub txtWorkFrom_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWorkFrom.Enter

        'if textbox is unlock
        If Not chkLock.Checked Then

            '' set message text
            'lblMessage.Text = "Format hour hh:mm"

            'check if string text in textbox is different null 
            If Not String.IsNullOrEmpty(txtWorkFrom.Text) Then

                ' convert string format hour hh:mm to integer
                txtWorkFrom.Text = ConvertStrHourToInt(txtWorkFrom.Text)

            End If

        End If

    End Sub

    Private Sub txtWorkTo_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtWorkTo.KeyPress

        ' limit length of text less than 4 letters
        txtWorkTo.MaxLength = 4

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub

    Private Sub txtWorkTo_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWorkTo.Leave

        ' if it's unlocked, we validate data
        If Not chkLock.Checked Then

            If Not String.IsNullOrWhiteSpace(txtWorkTo.Text) And txtWorkTo.Text.Length >= 3 Then

                ' validate hour and minute
                If Functions.ValidateStrHour(Double.Parse(txtWorkTo.Text.Trim()).ToString("0#:##")) Then

                    If Not String.IsNullOrEmpty(txtWorkFrom.Text) Then

                        If Integer.Parse(txtWorkTo.Text) > Integer.Parse(Functions.ConvertStrHourToInt(txtWorkFrom.Text)) Then

                            ' true, set value to textbox
                            txtWorkTo.Text = Double.Parse(txtWorkTo.Text).ToString("0#:##")

                            'set normal message
                            lblMessage.Text = normalMessage

                        Else

                            ' false, clear value
                            txtWorkTo.Text = Nothing

                            ' print error message
                            lblMessage.Text = erWorkTo1

                        End If

                    Else

                        ' set value to textbox
                        txtWorkTo.Text = Double.Parse(txtWorkTo.Text).ToString("0#:##")

                        'set normal message
                        lblMessage.Text = normalMessage

                    End If


                Else

                    ' false clear value
                    txtWorkTo.Text = Nothing

                    ' print error message
                    lblMessage.Text = erWrongFormat

                End If
            Else

                ' false clear value
                txtWorkTo.Text = Nothing

            End If

            '' set value for message is null
            'lblMessage.Text = Nothing

        End If

    End Sub

    Private Sub txtWorkTo_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWorkTo.Enter

        'if textbox is unlock
        If Not chkLock.Checked Then

            '' set message text
            'lblMessage.Text = "Format hour hh:mm"

            'check if string text in textbox is different null 
            If Not String.IsNullOrEmpty(txtWorkTo.Text) Then

                ' convert string format hour hh:mm to integer
                txtWorkTo.Text = ConvertStrHourToInt(txtWorkTo.Text)

            End If

        End If

    End Sub

    Private Sub txtMidFrom_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMidFrom.KeyPress

        ' limit length of text less than 4 letters
        txtMidFrom.MaxLength = 4

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub

    Private Sub txtMidFrom_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMidFrom.Leave

        ' if it's unlocked, we validate data
        If Not chkLock.Checked Then

            If Not String.IsNullOrWhiteSpace(txtMidFrom.Text) And txtMidFrom.Text.Length >= 3 Then

                ' validate hour and minute
                If Functions.ValidateStrHour(Double.Parse(txtMidFrom.Text.Trim()).ToString("0#:##")) Then

                    ' true, set value to textbox
                    txtMidFrom.Text = Double.Parse(txtMidFrom.Text).ToString("0#:##")

                    'set normal message
                    lblMessage.Text = normalMessage

                Else

                    ' false clear value
                    txtMidFrom.Text = Nothing

                End If
            Else

                ' false clear value
                txtMidFrom.Text = Nothing

            End If

            '' set value for message is null
            'lblMessage.Text = Nothing

        End If

    End Sub

    Private Sub txtMidFrom_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMidFrom.Enter

        'if textbox is unlock
        If Not chkLock.Checked Then

            '' set message text
            'lblMessage.Text = "Format hour hh:mm"

            'check if string text in textbox is different null 
            If Not String.IsNullOrEmpty(txtMidFrom.Text) Then

                ' convert string format hour hh:mm to integer
                txtMidFrom.Text = ConvertStrHourToInt(txtMidFrom.Text)

            End If

        End If

    End Sub

    Private Sub txtMidTo_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMidTo.Leave

        ' if it's unlocked, we validate data
        If Not chkLock.Checked Then

            If Not String.IsNullOrWhiteSpace(txtMidTo.Text) And txtMidTo.Text.Length >= 3 Then

                ' validate hour and minute
                If Functions.ValidateStrHour(Double.Parse(txtMidTo.Text.Trim()).ToString("0#:##")) Then

                    ' true, set value to textbox
                    txtMidTo.Text = Double.Parse(txtMidTo.Text).ToString("0#:##")

                    'set normal message
                    lblMessage.Text = normalMessage

                Else

                    ' false clear value
                    txtMidTo.Text = Nothing

                End If
            Else

                ' false clear value
                txtMidTo.Text = Nothing

            End If

            '' set value for message is null
            'lblMessage.Text = Nothing

        End If

    End Sub

    Private Sub txtMidTo_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMidTo.KeyPress

        ' limit length of text less than 4 letters
        txtMidTo.MaxLength = 4

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub

    Private Sub txtMidTo_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMidTo.Enter

        'if textbox is unlock
        If Not chkLock.Checked Then

            '' set message text
            'lblMessage.Text = "Format hour hh:mm"

            'check if string text in textbox is different null 
            If Not String.IsNullOrEmpty(txtMidTo.Text) Then

                ' convert string format hour hh:mm to integer
                txtMidTo.Text = ConvertStrHourToInt(txtMidTo.Text)

            End If

        End If

    End Sub

    Private Sub txtDelimitation_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDelimitation.Leave

        ' if it's unlocked, we validate data
        If Not chkLock.Checked Then

            If Not String.IsNullOrWhiteSpace(txtDelimitation.Text) And txtDelimitation.Text.Length >= 3 Then

                ' validate hour and minute
                If Functions.ValidateStrHour(Double.Parse(txtDelimitation.Text.Trim()).ToString("0#:##")) Then

                    If Not String.IsNullOrEmpty(txtWorkFrom.Text) And Not String.IsNullOrEmpty(txtWorkTo.Text) Then

                        If Integer.Parse(txtDelimitation.Text) > Integer.Parse(Functions.ConvertStrHourToInt(txtWorkFrom.Text)) And
                            Integer.Parse(txtDelimitation.Text) < Integer.Parse(Functions.ConvertStrHourToInt(txtWorkTo.Text)) Then

                            ' true, set value to textbox
                            txtDelimitation.Text = Double.Parse(txtDelimitation.Text).ToString("0#:##")

                            'set normal message
                            lblMessage.Text = normalMessage

                        Else

                            'false clear value
                            txtDelimitation.Text = Nothing

                            ' print error message
                            lblMessage.Text = erDelimit2

                        End If

                    ElseIf Not String.IsNullOrEmpty(txtWorkFrom.Text) Then

                        If Integer.Parse(txtDelimitation.Text) > Integer.Parse(Functions.ConvertStrHourToInt(txtWorkFrom.Text)) Then

                            ' true, set value to textbox
                            txtDelimitation.Text = Double.Parse(txtDelimitation.Text).ToString("0#:##")

                            'set normal message
                            lblMessage.Text = normalMessage

                        Else

                            'false, clear value
                            txtDelimitation.Text = Nothing

                            ' print error message
                            lblMessage.Text = erDelimit3

                        End If

                    ElseIf Not String.IsNullOrEmpty(txtWorkTo.Text) Then

                        If Integer.Parse(txtDelimitation.Text) < Integer.Parse(Functions.ConvertStrHourToInt(txtWorkTo.Text)) Then

                            ' true, set value to textbox
                            txtDelimitation.Text = Double.Parse(txtDelimitation.Text).ToString("0#:##")

                            'set normal message
                            lblMessage.Text = normalMessage

                        Else

                            'false, clear value
                            txtDelimitation.Text = Nothing

                            ' print error message
                            lblMessage.Text = erDelimit4

                        End If

                    Else

                        ' set value to textbox
                        txtDelimitation.Text = Nothing

                        lblMessage.Text = erDelimit1


                    End If

                Else

                    ' false clear value
                    txtDelimitation.Text = Nothing

                End If
            Else

                ' false clear value
                txtDelimitation.Text = Nothing

            End If

            '' set value for message is null
            'lblMessage.Text = Nothing

        End If

    End Sub

    Private Sub txtDelimitation_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDelimitation.KeyPress

        ' limit length of text less than 4 letters
        txtDelimitation.MaxLength = 4

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub

    Private Sub txtDelimitation_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDelimitation.Enter

        'if textbox is unlock
        If Not chkLock.Checked Then

            '' set message text
            'lblMessage.Text = "Format hour hh:mm"

            'check if string text in textbox is different null 
            If Not String.IsNullOrEmpty(txtDelimitation.Text) Then

                ' convert string format hour hh:mm to integer
                txtDelimitation.Text = ConvertStrHourToInt(txtDelimitation.Text)

            End If

        End If

    End Sub

    Private Sub grdRestTime_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles grdRestTime.CellBeginEdit

        ' check if cell isn't empty
        If Not grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value Is Nothing Then

            ' convert string format time to integer
            grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value = Functions.ConvertStrHourToInt(grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value)

        End If

    End Sub

    Private Sub grdRestTime_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdRestTime.CellEndEdit

        editedResttime = True

        ' check if cell isn't empty
        If Not grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value Is Nothing Then

            'validate string time
            If Functions.ValidateStrHour(Integer.Parse(grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value).ToString("0#:##")) Then

                ' column rest from
                If e.ColumnIndex = 0 Then

                    ' check if cell column rest to not empty
                    If Not grdRestTime.Rows.Item(e.RowIndex).Cells.Item(1).Value Is Nothing Then

                        ' check if rest from < rest to
                        If grdRestTime.Rows.Item(e.RowIndex).Cells.Item(0).Value < Functions.ConvertStrHourToInt(grdRestTime.Rows.Item(e.RowIndex).Cells.Item(1).Value) Then

                            'convert integer to string format time
                            grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value = Integer.Parse(grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value).ToString("0#:##")

                        Else

                            ' clear wrong value
                            grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value = Nothing

                            ' print error message
                            lblMessage.Text = erResttime1

                        End If

                    Else

                        'convert integer to string format time
                        grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value = Integer.Parse(grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value).ToString("0#:##")

                    End If

                ElseIf e.ColumnIndex = 1 Then

                    ' check if cell column rest from not empty
                    If Not grdRestTime.Rows.Item(e.RowIndex).Cells.Item(0).Value Is Nothing Then

                        ' check if rest from < rest to
                        If grdRestTime.Rows.Item(e.RowIndex).Cells.Item(1).Value > Functions.ConvertStrHourToInt(grdRestTime.Rows.Item(e.RowIndex).Cells.Item(0).Value) Then

                            'convert integer to string format time
                            grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value = Integer.Parse(grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value).ToString("0#:##")

                        Else

                            ' clear wrong value
                            grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value = Nothing

                            'print error message
                            lblMessage.Text = erResttime2

                        End If

                    Else

                        'convert integer to string format time
                        grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value = Integer.Parse(grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value).ToString("0#:##")

                    End If

                End If

            Else

                ' clear wrong value
                grdRestTime.Rows.Item(e.RowIndex).Cells.Item(e.ColumnIndex).Value = Nothing

            End If

        End If
    End Sub

    Private Sub grdRestTime_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdRestTime.EditingControlShowing

        Dim control As Control = e.Control

        'add handler keypress for datagridview rest time
        AddHandler control.KeyPress, AddressOf Handle_KeyPress

    End Sub

    ''' <summary>
    ''' handle keypress event in datagridview rest time
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Handle_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)

        Dim txtCell As TextBox = sender

        ' set text length max is 4
        txtCell.MaxLength = 4

        ' check allow only number and control
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else

            e.Handled = True

        End If

    End Sub


    ''' <summary>
    ''' Modify Timetable detail(s). Set timetable's status field is 1 (is deleted).
    ''' And add a Timetable have same "No" as timetable was deleted, but it's different from expiration date 
    ''' from and date to. 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click

        ' create a data tranfer object timetable
        Dim dtoTimetable As DTOTimetable = New DTOTimetable()

        ' flag = true if dto timetable is valid 
        Dim flag As Boolean = True

        ' timetable in future
        Dim inFuture As Boolean = False

        ' timetable no
        dtoTimetable.TimetableNoProperty = txtTimetableNo.Text

        ' validate  txt timetable name
        If Not String.IsNullOrEmpty(txtTimetableName.Text) Then

            ' timetable name
            dtoTimetable.TimetableNameProperty = txtTimetableName.Text

            ' clear message if had
            lblMessage.Text = Nothing


            'validate txt work from 
            If Not String.IsNullOrEmpty(txtWorkFrom.Text) Then

                ' timetable work from
                dtoTimetable.WorkFromProperty = Functions.ConvertStrHourToInt(txtWorkFrom.Text)

                ' clear message if had
                lblMessage.Text = Nothing


                ' validate txt work to
                If Not String.IsNullOrEmpty(txtWorkTo.Text) Then

                    ' timetable work to
                    dtoTimetable.WorkToProperty = Functions.ConvertStrHourToInt(txtWorkTo.Text)

                    ' clear message if had
                    lblMessage.Text = Nothing


                    ' validate txt delimitation
                    If Not String.IsNullOrEmpty(txtDelimitation.Text) Then

                        ' timetable delimitation
                        dtoTimetable.DelimitationProperty = Functions.ConvertStrHourToInt(txtDelimitation.Text)

                        ' clear message if had
                        lblMessage.Text = Nothing


                        ' validate cbo unit minute
                        If Not String.IsNullOrEmpty(cboUnit.Text) Then

                            ' unit minute
                            dtoTimetable.UnitProperty = cboUnit.Text

                            ' clear message if had
                            lblMessage.Text = Nothing


                            ' validate cbo  OT unit minute
                            If Not String.IsNullOrEmpty(cboOtUnit.Text) Then

                                'OT unit minute
                                dtoTimetable.OtUnitProperty = cboOtUnit.Text

                                ' clear message if had
                                lblMessage.Text = Nothing


                                ' timetable rest time
                                If ValidateInputRestTime() Then

                                    For i As Integer = 0 To grdRestTime.Rows.Count - 2

                                        If Not grdRestTime.Rows.Item(i).Cells.Item(0).Value Is Nothing And
                                            Not grdRestTime.Rows.Item(i).Cells.Item(1).Value Is Nothing Then

                                            'create new row
                                            Dim row As DataRow = dtoTimetable.DtRestTimeProperty.NewRow()


                                            ' rest from
                                            row.Item(0) = Functions.ConvertStrHourToInt(grdRestTime.Rows.Item(i).Cells.Item(0).Value)
                                            Console.WriteLine(row.Item(0))

                                            ' rest to
                                            row.Item(1) = Functions.ConvertStrHourToInt(grdRestTime.Rows.Item(i).Cells.Item(1).Value)
                                            Console.WriteLine(row.Item(1))

                                            ' add row
                                            dtoTimetable.DtRestTimeProperty.Rows.Add(row)

                                        End If

                                    Next

                                    ' clear message if had
                                    lblMessage.Text = Nothing


                                    ' timetable expiration from
                                    If Not String.IsNullOrEmpty(txtExFrom.Text) Then

                                        ' check if ex to old = 99999999 if new ex from < date now then print error else get value. 
                                        If dtoCurrTimetable.ExToProperty.Equals("99999999") Then

                                            ' check if ex from old not equal 0
                                            If Not dtoCurrTimetable.ExFromProperty.Equals("0") Then

                                                'check  if ex from old <= now
                                                If DateTime.ParseExact(Functions.ConvertstrDBDateToStrDate(dtoCurrTimetable.ExFromProperty), "d", provider) <= Date.Now Then

                                                    ' check new expiration from
                                                    If DateTime.ParseExact(txtExFrom.Text, "d", provider) > Date.Now Then

                                                        dtoTimetable.ExFromProperty = Functions.ConvertStrDateToStrDBDate(txtExFrom.Text)

                                                        ' clear message if had
                                                        lblMessage.Text = Nothing

                                                        ' check ex to not null
                                                        If Not String.IsNullOrEmpty(txtExTo.Text) Then

                                                            dtoTimetable.ExToProperty = Functions.ConvertStrDateToStrDBDate(txtExTo.Text)

                                                        Else

                                                            dtoTimetable.ExToProperty = "99999999"

                                                        End If


                                                    Else

                                                        ' set flag
                                                        flag = False

                                                        txtExFrom.Focus()

                                                        ' print error message
                                                        lblMessage.Text = "Data expiration date from input must be greater than date now!"

                                                    End If

                                                    'ex from old > now
                                                Else

                                                    ' check new expiration from
                                                    If DateTime.ParseExact(txtExFrom.Text, "d", provider) > Date.Now Then

                                                        dtoTimetable.ExFromProperty = Functions.ConvertStrDateToStrDBDate(txtExFrom.Text)

                                                        ' clear message if had
                                                        lblMessage.Text = Nothing

                                                        ' check ex to not null
                                                        If Not String.IsNullOrEmpty(txtExTo.Text) Then

                                                            dtoTimetable.ExToProperty = Functions.ConvertStrDateToStrDBDate(txtExTo.Text)

                                                        Else

                                                            dtoTimetable.ExToProperty = "99999999"

                                                        End If

                                                        inFuture = True

                                                    Else

                                                        ' set flag
                                                        flag = False

                                                        txtExFrom.Focus()

                                                        ' print error message
                                                        lblMessage.Text = "Data expiration date from input must be greater than date now!"

                                                    End If



                                                End If

                                                ' ex from old = 0
                                            Else

                                                ' check new expiration from
                                                If DateTime.ParseExact(txtExFrom.Text, "d", provider) > Date.Now Then

                                                    dtoTimetable.ExFromProperty = Functions.ConvertStrDateToStrDBDate(txtExFrom.Text)

                                                    ' clear message if had
                                                    lblMessage.Text = Nothing

                                                    ' check ex to not null
                                                    If Not String.IsNullOrEmpty(txtExTo.Text) Then

                                                        dtoTimetable.ExToProperty = Functions.ConvertStrDateToStrDBDate(txtExTo.Text)

                                                    Else

                                                        dtoTimetable.ExToProperty = "99999999"

                                                    End If


                                                Else

                                                    ' set flag
                                                    flag = False

                                                    txtExFrom.Focus()

                                                    ' print error message
                                                    lblMessage.Text = "Data expiration date from input must be greater than date now!"

                                                End If

                                            End If



                                        Else

                                            Dim oldExTo As Date = DateTime.ParseExact(Functions.ConvertstrDBDateToStrDate(dtoCurrTimetable.ExToProperty), "d", provider)

                                            Dim exFrom As Date = DateTime.ParseExact(txtExFrom.Text, "d", provider)

                                            ' check old ex to < date.Now
                                            If oldExTo < Date.Now Then

                                                'check new ex from > date.now
                                                If exFrom > Date.Now Then

                                                    dtoTimetable.ExFromProperty = Functions.ConvertStrDateToStrDBDate(txtExFrom.Text)

                                                    ' clear message if had
                                                    lblMessage.Text = Nothing

                                                Else

                                                    ' set flag
                                                    flag = False

                                                    txtExFrom.Focus()

                                                    ' print error message
                                                    lblMessage.Text = "Data expiration date from input must be greater than now!"

                                                End If

                                                'old ex to >= date now
                                            Else ' KIEM TRA XEM CO CAN PHAI CHECK OLD EX FROM

                                                ' check if ex from old not equal 0
                                                If Not dtoCurrTimetable.ExFromProperty.Equals("0") Then

                                                    'check if ex from old <= now
                                                    If DateTime.ParseExact(Functions.ConvertstrDBDateToStrDate(dtoCurrTimetable.ExFromProperty), "d", provider) <= Date.Now Then

                                                        'check if new ex date from > old ex date to it's ok else false
                                                        If exFrom > oldExTo Then

                                                            dtoTimetable.ExFromProperty = Functions.ConvertStrDateToStrDBDate(txtExFrom.Text)

                                                            ' clear message if had
                                                            lblMessage.Text = Nothing

                                                        Else

                                                            ' set flag
                                                            flag = False

                                                            txtExFrom.Focus()

                                                            ' print error message
                                                            lblMessage.Text = "Data expiration date from input must be greater than current timetable!"

                                                        End If

                                                        'ex from old > now
                                                    Else

                                                        'check new ex from > date.now
                                                        If exFrom > Date.Now Then

                                                            dtoTimetable.ExFromProperty = Functions.ConvertStrDateToStrDBDate(txtExFrom.Text)

                                                            ' clear message if had
                                                            lblMessage.Text = Nothing


                                                            inFuture = True

                                                        Else

                                                            ' set flag
                                                            flag = False

                                                            txtExFrom.Focus()

                                                            ' print error message
                                                            lblMessage.Text = "Data expiration date from input must be greater than now!"

                                                        End If

                                                    End If

                                                Else

                                                    'check if new ex date from > old ex date to it's ok else false
                                                    If exFrom > oldExTo Then

                                                        dtoTimetable.ExFromProperty = Functions.ConvertStrDateToStrDBDate(txtExFrom.Text)

                                                        ' clear message if had
                                                        lblMessage.Text = Nothing

                                                    Else

                                                        ' set flag
                                                        flag = False

                                                        txtExFrom.Focus()

                                                        ' print error message
                                                        lblMessage.Text = "Data expiration date from input must be greater than current timetable!"

                                                    End If


                                                End If


                                                End If

                                            End If

                                    Else

                                            dtoTimetable.ExFromProperty = Date.Now.AddDays(1).ToString("yyyyMMdd")

                                            ' check ex to not null
                                            If Not String.IsNullOrEmpty(txtExTo.Text) Then

                                                'check ex to > now + 1
                                                If DateTime.ParseExact(txtExTo.Text, "d", provider) > (Date.Now.AddDays(1)) Then

                                                    dtoTimetable.ExToProperty = Functions.ConvertStrDateToStrDBDate(txtExTo.Text)

                                                Else

                                                    'flag = False
                                                    flag = False

                                                    ' print error message
                                                    lblMessage.Text = "Data expiration date to input must be greater than date now + 1 error."

                                                End If

                                            Else

                                                dtoTimetable.ExToProperty = "99999999"

                                            End If

                                    End If



                                    ' timetable abbreviation
                                    If Not String.IsNullOrEmpty(txtAbrev.Text) Then

                                        dtoTimetable.AbreviationProperty = txtAbrev.Text

                                    Else

                                        dtoTimetable.AbreviationProperty = "NULL"

                                    End If




                                    ' timetable midnight from
                                    If Not String.IsNullOrEmpty(txtMidFrom.Text) Then

                                        dtoTimetable.MidFromProperty = Functions.ConvertStrHourToInt(txtMidFrom.Text)

                                    Else

                                        dtoTimetable.MidFromProperty = "NULL"

                                    End If

                                    ' timetable midnight to
                                    If Not String.IsNullOrEmpty(txtMidTo.Text) Then

                                        dtoTimetable.MidToProperty = Functions.ConvertStrHourToInt(txtMidTo.Text)

                                    Else

                                        dtoTimetable.MidToProperty = "NULL"

                                    End If


                                Else

                                    ' set flag 
                                    flag = False

                                    ' print message
                                    lblMessage.Text = "Rest time is not enough information."

                                End If

                            Else

                                ' set flag 
                                flag = False

                                ' print message and focus on it
                                cboOtUnit.Focus()

                                lblMessage.Text = "'OT Unit Minute' is not empty."

                            End If

                        Else

                            ' set flag 
                            flag = False

                            ' print message and focus on it
                            cboUnit.Focus()

                            lblMessage.Text = "'Unit Minute' is not empty."

                        End If

                    Else

                        ' set flag 
                        flag = False

                        ' print message and focus on it
                        txtDelimitation.Focus()

                        lblMessage.Text = "'Delimitation' is not empty."

                    End If

                Else

                    ' set flag 
                    flag = False

                    ' print message and focus on it
                    txtWorkTo.Focus()

                    lblMessage.Text = "'Work To' is not empty."

                End If

            Else

                ' set flag 
                flag = False

                ' print message and focus on it
                txtWorkFrom.Focus()

                lblMessage.Text = "'Work From' is not empty."

            End If

        Else

            ' set flag 
            flag = False

            ' print message and focus on it
            txtTimetableName.Focus()

            lblMessage.Text = "'Timetable Name' is not empty."

        End If

        ' check timetable valid
        If flag Then

            ' check if no change
            If CheckEditedTimetable(dtoCurrTimetable, dtoTimetable) Then

                ' check if timetable in future
                If inFuture Then

                    If bsTimetable.ModifyTimetableFuture(dtoTimetable, dtoCurrTimetable) Then


                        ' print message success
                        lblMessage.Text = "Update Timetable success."

                        ' reload dtTimetable
                        dtTimetable = bsTimetable.GetTimetable()

                        'reload datagridview timetable
                        LoadGrdTimetable()

                        ' clear details timetable
                        ClearDetails()

                        ' set edit rest time
                        editedResttime = False

                    Else

                        lblMessage.Text = "Update Timetable error."

                    End If

                Else
                    Dim newExTo As String

                    ' check to determine dto current timetable new ex to  
                    If dtoCurrTimetable.ExToProperty.Equals("99999999") Then

                        newExTo = Date.ParseExact(Functions.ConvertstrDBDateToStrDate(dtoTimetable.ExFromProperty), "d", provider).AddDays(-1).ToString("yyyyMMdd")

                    Else

                        If Integer.Parse(dtoCurrTimetable.ExToProperty) < Integer.Parse(dtoTimetable.ExFromProperty) Then

                            newExTo = dtoCurrTimetable.ExToProperty

                        Else

                            newExTo = newExTo = newExTo = Date.ParseExact(Functions.ConvertstrDBDateToStrDate(dtoTimetable.ExFromProperty), "d", provider).AddDays(-1).ToString("yyyyMMdd")

                        End If

                    End If


                    If bsTimetable.ModifyTimetable(dtoTimetable, dtoCurrTimetable, newExTo) Then


                        ' print message success
                        lblMessage.Text = "Update Timetable success."

                        ' reload dtTimetable
                        dtTimetable = bsTimetable.GetTimetable()

                        'reload datagridview timetable
                        LoadGrdTimetable()

                        ' clear details timetable
                        ClearDetails()

                        ' set edit rest time
                        editedResttime = False

                    Else

                        lblMessage.Text = "Update Timetable error."

                    End If

                End If

            Else

                ' print message no change
                lblMessage.Text = "No change in Timetable."

            End If

        End If


    End Sub


    ''' <summary>
    ''' check  difference timetable
    ''' </summary>
    ''' <param name="dtoOTimetable"></param>
    ''' <param name="dtoNTimetable"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckEditedTimetable(ByVal dtoOTimetable As DTOTimetable, ByVal dtoNTimetable As DTOTimetable) As Boolean

        If Not dtoOTimetable.TimetableNameProperty.Equals(dtoNTimetable.TimetableNameProperty) Or
          Not dtoOTimetable.ExFromProperty.Equals(dtoNTimetable.ExFromProperty) Or
          Not dtoOTimetable.ExToProperty.Equals(dtoNTimetable.ExToProperty) Or
          Not dtoOTimetable.WorkFromProperty.Equals(dtoNTimetable.WorkFromProperty) Or
         Not dtoOTimetable.WorkToProperty.Equals(dtoNTimetable.WorkToProperty) Or
         Not dtoOTimetable.DelimitationProperty.Equals(dtoNTimetable.DelimitationProperty) Or
            editedResttime Then

            Return True

        Else

            Return False

        End If
    End Function




    Private Sub GetDtoTimetable(ByRef dtoTimetable As DTOTimetable)

        dtoTimetable.TimetableNoProperty = txtTimetableNo.Text

        dtoTimetable.TimetableNameProperty = txtTimetableName.Text

        If Not String.IsNullOrEmpty(txtExFrom.Text) Then

            dtoTimetable.ExFromProperty = Functions.ConvertStrDateToStrDBDate(txtExFrom.Text)

        Else

            'default value
            dtoTimetable.ExFromProperty = "0"

        End If

        If Not String.IsNullOrEmpty(txtExTo.Text) Then

            dtoTimetable.ExToProperty = Functions.ConvertStrDateToStrDBDate(txtExTo.Text)

        Else

            ' default value
            dtoTimetable.ExToProperty = "99999999"

        End If

        dtoTimetable.AbreviationProperty = txtAbrev.Text

        dtoTimetable.WorkFromProperty = Functions.ConvertStrHourToInt(txtWorkFrom.Text)

        dtoTimetable.WorkToProperty = Functions.ConvertStrHourToInt(txtWorkTo.Text)

        dtoTimetable.UnitProperty = cboUnit.Text

        dtoTimetable.OtUnitProperty = cboOtUnit.Text

        dtoTimetable.DelimitationProperty = Functions.ConvertStrHourToInt(txtDelimitation.Text)

        If Not String.IsNullOrEmpty(txtMidFrom.Text) Then

            dtoTimetable.MidFromProperty = Functions.ConvertStrHourToInt(txtMidFrom.Text)

        End If

        If Not String.IsNullOrEmpty(txtMidTo.Text) Then

            dtoTimetable.MidToProperty = Functions.ConvertStrHourToInt(txtMidTo.Text)

        End If


    End Sub

    ''' <summary>
    ''' Get datatable rest time to dto timetable, in this case dto timetable is current dto timetable 
    ''' </summary>
    ''' <param name="dtoTimetable"></param>
    ''' <remarks></remarks>
    Private Sub GetRestTimeToDto(ByRef dtoTimetable As DTOTimetable)

        For i As Integer = 0 To grdRestTime.Rows.Count - 2

            'create new row
            Dim row As DataRow = dtoTimetable.DtRestTimeProperty.NewRow()

            ' rest from
            row.Item(0) = Functions.ConvertStrHourToInt(grdRestTime.Rows.Item(i).Cells.Item(0).Value)
            Console.WriteLine(row.Item(0))

            ' rest to
            row.Item(1) = Functions.ConvertStrHourToInt(grdRestTime.Rows.Item(i).Cells.Item(1).Value)
            Console.WriteLine(row.Item(1))

            ' add row
            dtoTimetable.DtRestTimeProperty.Rows.Add(row)

        Next

    End Sub


    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        
        ' check if timetable status = 1 is deleted.
        For Each row As DataRow In dtTimetable.Rows

            If dtoCurrTimetable.TimetableNoProperty.Equals(row.Item("TIME_TABLE_NO").ToString()) And
                dtoCurrTimetable.ExFromProperty.Equals(row.Item("EXPIRATION_FROM").ToString()) And
                dtoCurrTimetable.ExToProperty.Equals(row.Item("EXPIRATION_TO").ToString()) Then


                ' is exist
                If row.Item("STATUS").ToString().Equals("0") Then

                    'check if timetable is using
                    If Not bsTimetable.CheckTimetableUsing(dtoCurrTimetable) Then

                        'check if timetable had expired.
                        If Not dtoCurrTimetable.ExToProperty.Equals("99999999") Then

                            ' check if timetable expiration to > date now then do not change expiration date to is date now.
                            If DateTime.ParseExact(Functions.ConvertstrDBDateToStrDate(dtoCurrTimetable.ExToProperty), "d", New CultureInfo("fr-FR")) > Date.Now Then

                                ' check if timetable ex from < now
                                If DateTime.ParseExact(Functions.ConvertstrDBDateToStrDate(dtoCurrTimetable.ExFromProperty), "d", New CultureInfo("fr-FR")) <= Date.Now Then

                                    ' execute delete set ex to
                                    If bsTimetable.DeleteTimetable(dtoCurrTimetable, Date.Now.ToString("yyyyMMdd")) Then

                                        ' print message
                                        lblMessage.Text = "Delete timetable success."

                                        dtTimetable = bsTimetable.GetTimetable()

                                        LoadGrdTimetable()

                                    Else

                                        'print error message
                                        lblMessage.Text = "Delete timetable error!"

                                    End If

                                    'delete without change expiration date to. ex to and ex from > Now
                                Else
                                    ' xoa lun
                                    If bsTimetable.DeleteTimetableFuture(dtoCurrTimetable) Then

                                        ' print message
                                        lblMessage.Text = "Delete timetable success."

                                        dtTimetable = bsTimetable.GetTimetable()

                                        LoadGrdTimetable()

                                    Else

                                        'print error message
                                        lblMessage.Text = "Delete timetable error!"

                                    End If

                                End If

                                'delete without change expiration date to.
                            Else

                                If bsTimetable.DeleteTimetableExpired(dtoCurrTimetable) Then

                                    ' print message
                                    lblMessage.Text = "Delete timetable success."

                                    dtTimetable = bsTimetable.GetTimetable()

                                    LoadGrdTimetable()

                                Else

                                    'print error message
                                    lblMessage.Text = "Delete timetable error!"

                                End If

                            End If

                            ' expiration to = 99999999
                        Else

                            ' execute delete
                            If bsTimetable.DeleteTimetable(dtoCurrTimetable, Date.Now.ToString("yyyyMMdd")) Then

                                ' print message
                                lblMessage.Text = "Delete timetable success."

                                dtTimetable = bsTimetable.GetTimetable()

                                LoadGrdTimetable()

                            Else

                                'print error message
                                lblMessage.Text = "Delete timetable error!"

                            End If

                        End If

                        ' Timetable is using
                    Else

                        'print message
                        lblMessage.Text = "Timetable is using! Cannot delete."

                    End If

                Else

                    lblMessage.Text = "Timetable had been deleted."

                End If

                'exit loop
                Exit For

            End If

        Next

    End Sub


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtTimetableNo_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTimetableNo.KeyPress

        ' limit length of text less than 3 letters
        txtTimetableNo.MaxLength = 3

        ' check only allow numeric
        If (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then

            e.Handled = False

        Else
            ' cancel
            e.Handled = True

        End If

    End Sub

End Class